package com.zxl.live.tools.analytics;

import android.os.Bundle;

import com.google.firebase.analytics.FirebaseAnalytics;
import com.zxl.live.tools.base.ActivityLifecycle;
import com.zxl.live.tools.common.ApplicationHelper;
import com.zxl.live.tools.tools.LogUtils;

/**
 * Created by Administrator on 2018/2/6.
 */

public class AnalyticsHelper {

    private static FirebaseAnalytics getFirebaseAnalytics() {
        FirebaseAnalytics analytics = ActivityLifecycle.getFirebaseAnalytics();
        if (null == null) {
            analytics = FirebaseAnalytics.getInstance(ApplicationHelper.getContext());
        }
        return analytics;
    }

    public static void logEvent(String event, String param) {
        logEvent(new AnalyticsHelper.LogEvent(event).addParam(param, 0));
    }

    public static void logEvent(LogEvent event) {
        try {
            getFirebaseAnalytics().logEvent(event.event, event.bundle);
            LogUtils.d("log: " + event.event + ", " + event.bundle);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static class LogEvent {

        private String event;

        private Bundle bundle;

        public LogEvent(String event) {
            this.event = event;
            bundle = new Bundle();
        }

        public LogEvent addParam(String key, Object value) {
            if (Integer.class.isInstance(value)) {
                bundle.putInt(key, (int) value);
            } else if (String.class.isInstance(value)) {
                bundle.putString(key, (String) value);
            } else if (Boolean.class.isInstance(value)) {
                bundle.putBoolean(key, (Boolean) value);
            } else {
                LogUtils.d("other: " + key + "， " + value + ", " + value.getClass());
            }
            return this;
        }

    }

}
